package com.tuniu.agents.common.message.adaper;

import com.tuniu.agents.common.enums.StreamMessageTypeEnum;
import com.tuniu.agents.common.message.*;
import com.tuniu.agents.util.JsonUtil;

import java.util.Arrays;
import java.util.List;

public class AdapterAgentNewResp {


    public static String sendCardMsg(BaseAgentNewResp cardMsg) {
        Card card = JsonUtil.fromJsonWithUrl(JsonUtil.toJsonWithUrl(cardMsg.getContent()), Card.class);
        List<String> productTypeList = Arrays.asList(card.getCardType().split("_"));
        return new AdapterCard()
                .setProductType(productTypeList.get(0))
                .setProductSubType(productTypeList.get(1))
                .setContent(card.getAnswer())
                .setData(card.getData().get(0))
                .setType("card")
                .setResponseId(cardMsg.getResponseId())
                .toEventString();
    }

    public static AdapterRecQuestion sendRecQuestion(BaseAgentNewResp questionMsg) {
        RecQuestion question = JsonUtil.fromJsonWithUrl(JsonUtil.toJsonWithUrl(questionMsg.getContent()), RecQuestion.class);
        return new AdapterRecQuestion()
                .setProductType(question.getSubType())
                .setContent(question.getContent())
                .setType("recQuestion");
    }


    public static String sendErrorMsg(BaseAgentNewResp errorMsg) {
        return new AdapterText().setContent(errorMsg.getMessage()).toEventString();
    }

    public static String sendAnalysisMsg(BaseAgentNewResp analysisMsg) {
        Analysis analysis = JsonUtil.fromJson(JsonUtil.toJson(analysisMsg.getContent()), Analysis.class);
        return

                new AdapterAnalysis()
                .setContent(analysis.getAnswer())
                .setAnalysisDoneFlag(analysis.getAnalysisDoneFlag())
                        .setType("analysis")
                        .setResponseId(analysisMsg.getResponseId())
                .toEventString();
    }

    public static String sendThinkingMsg(BaseAgentNewResp answerMsg) {
        Answer answer = JsonUtil.fromJson(JsonUtil.toJson(answerMsg.getContent()), Answer.class);
        return

                new AdapterText().setContent(answer.getAnswer())
                        .setType(StreamMessageTypeEnum.THINKING.getValue())
                        .setResponseId(answerMsg.getResponseId())
                        .toEventString();
    }

    public static String sendAnswerMsg(BaseAgentNewResp answerMsg) {
        Answer answer = JsonUtil.fromJson(JsonUtil.toJson(answerMsg.getContent()), Answer.class);
        return

                new AdapterText().setContent(answer.getAnswer())
                        .setType("text")
                        .setResponseId(answerMsg.getResponseId())
                        .toEventString();
    }

    public static String sendFooterMsg(BaseAgentNewResp answerMsg) {
        Answer answer = JsonUtil.fromJson(JsonUtil.toJson(answerMsg.getContent()), Answer.class);
        return

                new AdapterText().setContent(answer.getAnswer())
                        .setType("footer")
                        .setResponseId(answerMsg.getResponseId())
                        .toEventString();
    }

    public static String sendStopMsg(BaseAgentNewResp stopMsg) {
        Answer answer = JsonUtil.fromJson(JsonUtil.toJson(stopMsg.getContent()), Answer.class);
        return

                new AdapterText().setContent(answer.getAnswer())
                        .setType("stop")
                        .setResponseId(stopMsg.getResponseId())
                        .toEventString();
    }
}
